package com.dp.service.jdc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.CommentContent;
import com.dp.pojo.NewsVo;
import com.dp.service.impl3.BaseImpl;
import com.dp.vo.NewsContenttitle;
import com.dp.vo.TChannels;

@Service
@Transactional
public class NewsServiceImpl extends BaseImpl {
    
    
    /**根据图片新闻cid获取图片新闻图片多个RID
     * @param cid
     * @return
     */
    public List<Integer> getImgRids(int newsCid){
        List<Integer> newsRid = new ArrayList<Integer>();
        this.sql="select nc.rid from news_contenresource nc " +
                "where  nc.cid="+newsCid;
        try{
            
            List<Object> temp=this.em.createNativeQuery(sql).getResultList();
            for (Object o : temp) {
                newsRid.add(Integer.parseInt(this.getString(o)));
            }
        }catch (Exception e) {
            logger.error("NewsServiceImpl-->>getImgRids fail !!!" + e.getMessage());
            e.printStackTrace();
        }
        return newsRid;
    }
    
    /**新闻顶头栏目名称结构
     * 
     * @param channelId:栏目名称
     * @return channelNames上几级栏目名称列表
     */
   
    public List<TChannels> getChannelNames(String channelId){
        List<TChannels> channelNames = new ArrayList<TChannels>();
        TChannels tc = null; 
        do {
            tc = this.getChannelById(channelId);
            if(tc==null){
                break;
            }else {
                
                channelNames.add(tc);
                channelId = tc.getPreviousId();
            }
        } while (!tc.getLevel().equals("3"));
        try{
            Collections.reverse(channelNames);
        }catch (Exception e) {
            logger.error(" getChannelNames目录名称倒序失败!!! fail !!!" +e.getMessage());
            e.printStackTrace();
        }

          return  channelNames;
    }
    
    
    
    /**根据栏目id获取栏目
     * @param channelId栏目名称
     * @return 栏目内容
     */
    @SuppressWarnings("unchecked")
    public TChannels getChannelById(String channelId) {
        TChannels tc ;
        String hql = "SELECT t FROM TChannels t WHERE t.channelId ='"+channelId+"'";
        try {
            tc = new TChannels();
            tc = (TChannels) this.getSingleResult(hql);
            return tc;
            
        } catch (Exception e) {
            logger.error(" getChannelById" +e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    
    
    /**根据新闻contentid获取所属tchannel
     * @param contentid
     * @return
     */
    public TChannels getTchannelByNewsContentid(String contentid) {
        TChannels tmp = null;
        this.sql = "select tc.channel_id,tc.channel_name,tc.previous_id,tc.channel_type from t_channels tc,news_extends_channel nec ,news_extends_channel_title nect, news_contenttitle nc"
                   +" where nc.contentid = '"+contentid+"' and nc.cid=nect.cid and nect.channleid = nec.channleid and nec.channel_id=tc.channel_id";

        try {
            List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
            if(temp.size()!=0&&temp!=null){
                tmp = new TChannels();
                Object[] o = temp.get(0);
                tmp.setChannelId(this.getString(o[0]));
                tmp.setChannelName(this.getString(o[1]));
                tmp.setPreviousId(this.getString(o[2]));
                tmp.setChannelType(this.getString(o[3]));
                return tmp;
            }
        } catch (Exception e) {
            log.error(" getTchannelByNewsContentid 根据新闻ID获取新闻所属栏目 fail !!!" + e.getMessage());
            e.printStackTrace();

        }
        return tmp;
    }
    
    
    /**根据新闻ID获取新闻内容
     * @param contentid
     * @return
     */
    @SuppressWarnings("unchecked")
    public NewsVo getNewsByContentid(String contentid) {
        NewsVo tmp = null;
        this.sql = "SELECT n FROM NewsContenttitle n where n.contentid=:contentid";

        try {
            Query query = em.createQuery(sql);
            query.setParameter("contentid", contentid.trim());
            NewsContenttitle s = (NewsContenttitle) query.getSingleResult();
            if (s != null) {
                tmp = new NewsVo();
                tmp.setCid(s.getCid());
                tmp.setContentId(s.getContentid());
                tmp.setContentName(s.getContenttitle());
                tmp.setContenticon(s.getContenticon() + "");
                tmp.setContentInfo(s.getContenttext());
                tmp.setPubCompany(s.getContentsource());
                tmp.setContentType(s.getNewstype());
                tmp.setContentHtml(s.getContenthtml());
                tmp.setContentSummary(s.getContentsummary());
                tmp.setAllowScoring(s.getAllowScoring());
                tmp.setAllowComments(s.getAllowComments());
                tmp.setLastupdatetime(s.getUpdatetime());
                tmp.setWeburl(s.getWeburl());
            }
        } catch (Exception e) {
            log.error(" getNewsByCOntentid 获取新闻内容 fail !!!" + e.getMessage());
            e.printStackTrace();

        }
        return tmp;
    }
    /**获取新闻内容中html内容
     * @param newsVo
     * @return
     */
    public NewsVo conversionHtml(NewsVo newsVo){
        String htmlPath = newsVo.getContentHtml();
        File htmlFile = new File(htmlPath);
        if(htmlFile.exists()){
            try {
                int b;
                StringBuffer sb = new StringBuffer();
                InputStreamReader isr=new InputStreamReader(new FileInputStream(htmlFile),"UTF-8");
                while ((b = isr.read()) != -1)
                {
                 sb.append((char)b);
                }
                newsVo.setContentHtml(sb.toString());
                isr.close();
                
            } catch (Exception e) {
                log.error(" conversionHtml 获取新闻内容HTML fail !!!" + e.getMessage());
                e.printStackTrace();
            } 
        }
       return newsVo; 
    }
    

}